using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Client
{
	public class Controleur_Collection
	{
		private Collection m_Collection;
		private MainWindow m_Window;
		private Thread m_ThreadScan;
		private Controleur_Scan m_CtrlScan;
		private Semaphore m_SemScan;
		private string m_FiltreMain;
		
		public Collection Collection {
			get{return m_Collection;}	
		}
		
		public Controleur_Collection (MainWindow window) {			
			m_Window = window;
			Load();
			m_SemScan = new Semaphore(1, 1);
			m_FiltreMain = "Artist";
		}
		
		
		private void Load() {
			try {
				XmlSerializer xs = new XmlSerializer(typeof(Collection));
				using (StreamReader rd = new StreamReader("./Collection.xml")) {
					m_Collection = (Collection) xs.Deserialize(rd);
				}
			}
			catch {
				m_Collection = new Collection();
			}
		}
		
		private void Save() {
			try {
				XmlSerializer xs = new XmlSerializer(typeof(Collection));
				using (StreamWriter wr = new StreamWriter("./Collection.xml")) {
				    xs.Serialize(wr, m_Collection);
				}
			}
			catch {
				System.Console.WriteLine("Erreur Save Collecection");
			}
		}
	
	
		public void Scan(string path) {
			m_ThreadScan = new System.Threading.Thread((ThreadStart) delegate {ScanThread(path);});
			m_ThreadScan.Start();
		}
		
		private void ScanThread(string path) {
			m_CtrlScan = new Controleur_Scan(this);
		
			m_CtrlScan.Start();
			m_Collection.ScanFolder(path);
			
			m_SemScan.WaitOne();
			
			
			m_Collection.SortCollection();
			Save();
			
			WriteMainTree();
			m_CtrlScan.Stop();
			
			m_SemScan.Release();
		}
		
		
		public void CancelScan() {
			m_SemScan.WaitOne();
			m_ThreadScan.Abort();
			m_SemScan.Release();
		}
		
		public void WriteMainTree() {
			Dictionary<string, bool> albums = new Dictionary<string, bool>();
			List<string> listToAdd = new List<string>();
			
			if(m_FiltreMain =="Album")
				listToAdd = m_Collection.Albums;
			else if(m_FiltreMain == "Artist")
				listToAdd = m_Collection.Artists;
			
			foreach(string valueTmp in listToAdd) {
				Filtre filtre = FactoryFiltre.GetFiltre(m_FiltreMain, valueTmp, m_Collection);
				albums.Add(filtre.Label, filtre.IsSelected);
			}
			
			m_Window.DisplayMain(albums);
		}
		
		public void WriteMusicTree(string albumName) {
			Dictionary<string, bool> musics = new Dictionary<string, bool>();
			
			Filtre filtre = FactoryFiltre.GetFiltre(m_FiltreMain, albumName, m_Collection);
			foreach(Music music in filtre.Musics) {
				if(!musics.ContainsKey(music.Title))
					musics.Add(music.Title, music.IsSelected);
			}
			
			m_Window.DisplayMusic(musics);
		}
		
		public void SelectSwitchedAlbum(string albumName) {
			Filtre filtre = FactoryFiltre.GetFiltre(m_FiltreMain, albumName, m_Collection);
			
			filtre.IsSelected = !filtre.IsSelected;
		}
		
		
		public void SelectSwitchedMusic(string music) {
			//Folder albums = m_Collection.GetAlbumsContainingMusic(music); 
			//foreach(Folder fd in albums)
				//fd.SelectSwitched();
			//foreach(File fl in FindMusics(music, albums))
				//fl.SelectSwitched();
		}
		
		
	}
}

